package com.admin.kotlintest.activity

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.View
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.BmobUser
import cn.bmob.v3.BmobUser.getCurrentUser
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import com.admin.kotlintest.R
import com.admin.kotlintest.adapter.FunctionAdapter
import com.admin.kotlintest.base.BaseActivity
import com.admin.kotlintest.bean.*
import com.admin.kotlintest.config.FixedValue
import com.admin.kotlintest.config.MyApplication
import com.admin.kotlintest.utils.ImageUtils
import com.admin.kotlintest.utils.SystemUtils
import com.socks.library.KLog
import kotlinx.android.synthetic.main.activity_home_pager.*
import kotlinx.android.synthetic.main.layout_title.*
import org.jetbrains.anko.toast

/**
 * Created by 吴昶 on 2018/4/10.
 */
class HomeActivity: BaseActivity(){

    var functionAdapter:FunctionAdapter?=null
    var functions:MutableList<Funcation>?=null

    var handler=Handler(Handler.Callback {
        msg ->
        when(msg.what){

        }
        false
    })

    var runable= Runnable {
        functionAdapter!!.upData(functions!!)
    }

    override fun bindLayout(): Int {
        return R.layout.activity_home_pager
    }

    override fun initData() {
        tv_title.text="首页"
        iv_title_left.visibility= View.GONE
        functions=ArrayList<Funcation>()
        functionAdapter=FunctionAdapter(context,R.layout.layout_function_item,functions)
        gv_option.adapter=functionAdapter

        initNoticBoard()
        initFuncation()
        SystemUtils.initPush()
    }

    override fun initEvent() {
        gv_option.setOnItemClickListener { _, _, position, _ ->

            var function=functionAdapter!!.getItem(position)
            if(function.action != "") {
                try {
                    var action = Intent(function.action)
                    action.putExtra("objectId", function.objectId)
                    startActivity(action)
                }catch (e:Exception){
                    toast("您目前使用的版本不支持此功能，请更新到最新版使用")
                }
            }
        }
    }


    //获取通知
    fun initNoticBoard(){
        var query= BmobQuery<NoticBoard>()
        query.findObjects(object : FindListener<NoticBoard>(){
            override fun done(p0: MutableList<NoticBoard>?, p1: BmobException?) {
                //p1为空时执行
                p1?:let {
                    if (p0!!.size >= 1) {
                        var p: NoticBoard? = p0[p0.size - 1]
                        println(p!!.image!!.fileUrl.toString())
                        ImageUtils.GlideLoadImage(context, p.image!!.fileUrl.toString(), iv_notice_board, R.mipmap.ic_launcher, R.mipmap.ic_launcher)
                    } else {
                        //无通知
                    }
                }
                //p1不为空时执行
                p1?.let {
                    KLog.d(p1.message)
                }
            }
        })
    }

    fun initFuncation(){
        //根据当前的登陆的用户查询 用户类型表 获取当前用户对应的类型
        var myuser=BmobUser.getCurrentUser(MyUser::class.java)
        KLog.d(myuser.toString())
        var typeQuery=BmobQuery<UserType>()
        typeQuery.addWhereEqualTo("user",myuser)
//        typeQuery.include("user")
        typeQuery.include("type,user")//设置包括获取type对象内容
        typeQuery.findObjects(object :FindListener<UserType>(){
            override fun done(p0: MutableList<UserType>?, p1: BmobException?) {
                if(p1==null && p0!=null && p0.size>0){
                    KLog.d(p0[0].user.toString())
                    if(p0[0].user!!.disabled){
                        isDisabled()
                    }else {
                        initUserPermission(p0[0].type)
                    }
                }else{
                    KLog.d(p1!!.message)
                }
            }
        })
    }

    fun initUserPermission(type: Type?){
        KLog.d(type!!.toString())
        MyApplication.setSharedPreferencesString(FixedValue.TYPE_NAME,type!!.name)
        MyApplication.setSharedPreferencesString(FixedValue.TYPE_OBJECT,type!!.objectId)
        //根据用户的类型获取用户拥有的功能权限
        var query=BmobQuery<UserPermission>()
        query.addWhereEqualTo("type",type)
        query.include("function")//设置包括获取function对象内容
        query.findObjects(object :FindListener<UserPermission>(){
            override fun done(p0: MutableList<UserPermission>?, p1: BmobException?) {
                p1?:let {
                    p0?.let {
                        for (item in p0){
                            functions!!.add( item.function )
                        }
                        functionAdapter!!.notifyDataSetChanged()
                    }
                }
                p1?.let {
                    KLog.d(p1.message)
                }
            }
        })
        //查询通用的权限
        initNomalPermission()
    }

    /**
     * 初始化类型数据
     */
    fun initNomalPermission(){

        var fquery=BmobQuery<NomalPermission>()
        fquery.include("function")//设置包括获取function对象内容
         fquery.findObjects(object :FindListener<NomalPermission>(){
                         override fun done(p0: MutableList<NomalPermission>?, p1: BmobException?) {
                                p1?:let {
                                    p0?.let {
                                        p0.forEach {
                                            functions!!.add( it.function )
                                            functionAdapter!!.notifyDataSetChanged()
                                        }
                                    }
                                }

                             p1?.let {
                                 KLog.d(p1.message)
                             }

                         }
                     })
    }
}
